package com.itheima.edu.info.manager.service;

import com.itheima.edu.info.manager.dao.StudentDao;
import com.itheima.edu.info.manager.domain.Student;

public class StudentService {
    StudentDao studentDao= new StudentDao();

    public static boolean isEsists(String id ,StudentDao studentDao) {
        Student[] stus= studentDao.findAllStudent();
        //假设id在数组中不存在
        boolean exists= false;
        //遍历数组,获取每一个学生对象.准备进行判断
        for (int i = 0; i < stus.length; i++) {
            Student student=stus[i];
            if (student !=null&&student.getId().equals(id)){
                exists=true;
                break;
            }
        }
        return exists;
    }
    //根据id删除学生
    public static void deleteStudentById(String delId) {
        StudentDao.deleteStudentById(delId);
    }
    //根据id修改
    public static void updateStudent(String updateId, Student newStu) {
        StudentDao.updateStudent(updateId,newStu);
    }

    //添加学生方法
    public boolean addStudent(Student stu) {
        //穿件库管对象

        return studentDao.addStudent(stu);
    }
    //根据Id查询学生
    public Student[] findAllStudent() {
        //调用库管对象的findAllStudent获取学生对象数组
        Student[] allStudent = studentDao.findAllStudent();
        //判断数组中是否有学生信息(有 返回地址  没有返回null)
        //思路: 数组中只要存在一个不是null的元素,就代表没有学生写信息
        boolean flag=false;
        for (int i = 0; i < allStudent.length; i++) {
            Student stu= allStudent[i];
            if (stu!=null){
                flag=true;
                break;
            }
        }
        if (flag){
            //有信息返回数组
            return allStudent;
        }else {
            return null;
        }
    }
}
